// pages/mine/main/index.js
const app = getApp()
const utils = require('../../../utils/util.js')
import Toast from '../../../vant-weapp/dist/toast/toast'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    navHeight: app.globalData.navHeight,
    avatarUrl:'',
    userInfo: null,
    phone: null,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setupInit()
    // const session_key = wx.getStorageSync('session_key')
    // if (session_key){
    //   this.setupInitData()
    // }else{
    //   app.globalData.openIdHandler = ()=>{
    //     this.setupInitData()
    //   }
    // }
    if (typeof this.getTabBar === 'function' &&
      this.getTabBar()) {
      this.getTabBar().setData({
        selected: 4
      })
    }
  },

  setupInitData:function(){
    const phone = wx.getStorageSync('phone')
    if (phone) {
      const res = this.handlePhone(phone)
      this.setData({
        phone: res
      })
    }
    setTimeout(() => {
      this.getLocalUserInfo()
    }, 100)
  },
  
  handlePhone: function (phone = ''){
    const beforeNum = phone.substr(0, 3)
    const afterNum = phone.substr(7, phone.length - 7)

    return beforeNum + '****' + afterNum
  },

  setupInit: function () {
    utils.getFrameById('#navigation', (frame) => {
      if (frame && frame.height) {
        this.setData({
          navHeight: frame.height
        })
      }
    })
  },
  getLocalUserInfo:function(){
   const userInfo = wx.getStorageSync('userInfo')
    if (userInfo){
        this.setData({
          userInfo: userInfo,
          phone: userInfo.phone,
          avatarUrl: userInfo.headImg
        })
     }
  },
  // 获取电话号码
  bindgetphonenumber:function(e) {
    const { type = ''} = e.currentTarget.dataset
    const { encryptedData: encryptedDataPhone, errMsg, iv } = e.detail

    if (errMsg === 'getPhoneNumber:fail user deny' || errMsg === 'getPhoneNumber:fail:user deny'){
      Toast({
        position: 'top',
        message: '授权失败'
      });
      return false
    }
    if(type == "updatePhone")  {
      if(!this.data.phone || !this.data.userInfo) {
        Toast({
          position: 'top',
          message: '请登录'
        });
        return false
      }
      this.updatePhone(encryptedDataPhone,iv)
    } 
  },
  // 更新电话
  updatePhone(encryptedDataPhone,iv) {
    wx.login({
      success: res1 => {
        let code = JSON.parse(JSON.stringify(res1.code))
        utils.$http(app.globalData.api.updatePhone, {
          encryptedDataPhone,
          iv,
          code
        },"POST",false,"JSON").then(res=>{
          if(res.success === 1){
            let phone = res.msg
            wx.setStorageSync('phone',phone)
            this.setData({ phone })
          } else {
            const { code = '', msg } = res
            const message = code === "1000" ? '电话号码更换超时，请重新点击更换' : '该号码已注册，无法更换'
            wx.showToast({ mask: true, icon: 'none', duration: 2000,title: message });
          }
        })
      }
    })
  },
  cellTap:function(e){
    const type = parseInt(e.currentTarget.dataset.type)
    const userInfo = wx.getStorageSync('userInfo')
    const phone = wx.getStorageSync('phone')
    switch (type){
       case 1: //我的收藏
      //  const { phone,userInfo } = this.data
       if(userInfo && phone) {
          wx.navigateTo({
            url: '../collect/index',
          })
        } else {
          wx.showToast({ mask: true, title: '请登录并完善手机信息',icon: 'none',  duration: 2000  })
        }
        break
        case 6: //我的实景投稿
        //  const { phone,userInfo } = this.data
         if(userInfo && phone) {
            wx.navigateTo({
              url: '/pages/real/myLive/index',
            })
          } else {
            wx.showToast({ mask: true, title: '请登录并完善手机信息',icon: 'none',  duration: 2000  })
          }
          break
       case 2: // 零圆设计
          wx.navigateTo({
            url: '/pages/design/index/index',
          })
          // wx.switchTab({
          //   url: '/pages/design/main/index?url=xxxx',
          // })
        break
       case 3: // 推荐编辑
        wx.navigateTo({
          url: '/pages/mine/editTag/index',
        })
        break
       case 4: // 关注公众号
        wx.navigateTo({
          url: '/pages/mine/afficialAccount/index',
        })
        break
       case 5: // 评价
        wx.navigateTo({
          url: '/pages/evaluat/main/index',
        })
        break
       default:
        break
    }
  },
  jumpToRegister() {
    wx.navigateTo({
      url: '/pages/mine/register/index',
    })
  },

  onTabItemTap (item) {
    // console.log('tabBarItem:', item)

  },

  binderror:function(e){
    //  console.log("e:",e)
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
   
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
   if(!this.data.userInfo) this.getLocalUserInfo()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
 
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  }
})